home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nebula 2
/
Nebula Two.iso
/
SourceCode
/
MiscKit1.7.1
/
MiscKit
/
Examples
/
MiscStringRegex
/
test.m
< prev
Wrap
Text File
|
1995-04-12
|
3KB
|
80 lines
// Written by Carl Lindberg Copyright (c) 1994 by Carl Lindberg.
// Version 1.0. All rights reserved.
//
// This notice may not be removed from this source code.
//
// This object is included in the MiscKit by permission from the author
// and its use is governed by the MiscKit license, found in the file
// "LICENSE.rtf" in the MiscKit distribution. Please refer to that file
// for a list of all applicable permissions and restrictions.
//
#import <misckit/MiscString.h>
#define NUMREG 11
#define NUMSTR 2
void main() {
id ts2, tstr = [MiscString newWithString:" 678HelLo My dear 44bonnie.3"];
int i,j,spot,len,num,k;
id a = [MiscString new],b= [MiscString new],c= [MiscString new];
int numreg = NUMREG;
char *regarr[NUMREG] = {"[0-9]",
"[0-9]*",
"([0-9])+",
"[^0-9]+",
"[a-z]+",
"[a-zA-Z]+",
"[{][^}]*[}]",
NULL,
"[[0-9]**",
"[0-9]?[a-zA-Z]+",
"([^0-9]+)"};
char *strarr[NUMSTR] = {" 678HelLo My dear 44bonnie.3",
"678HelLo My de(**)ar 44bo{}nnie.3"};
for (k=0;k<NUMSTR;k++) {
[tstr setStringValue:strarr[k]];
for (i=0;i<numreg;i++) {
printf("String is : \"%s\"\n",[tstr stringValue]);
printf("numOf '%s' : %d\n",regarr[i],num = [tstr numOfRegex:regarr[i] caseSensitive:YES]);
printf("numOf '%s' nocase: %d\n",regarr[i],[tstr numOfRegex:regarr[i] caseSensitive:NO]);
printf("(-1 is MISC_STRING_LAST)\n");
for (j=-1;j<=num;j++) {
spot = [tstr grep:regarr[i] occurrenceNum:j caseSensitive:YES before:a middle:b after:c];
printf("%d: retval: %d before: '%s' middle: '%s' after: '%s'\n",j,spot,
[a stringValue],
[b stringValue],
[c stringValue]);
spot = [tstr spotOfRegex:regarr[i] occurrenceNum:j caseSensitive:YES length:&len];
printf("spot: %d len: %d\n",spot,len);
spot = [tstr rspotOfRegex:regarr[i] occurrenceNum:j caseSensitive:YES length:&len];
printf("rspot: %d len: %d\n",spot,len);
}
ts2 = [tstr copy];
spot = [ts2 replaceEveryOccurrenceOfRegex:regarr[i] with:"[]" caseSensitive:YES];
printf("After replace all '%s' with '[]': '%s'\n",regarr[i],[ts2 stringValue]);
printf("(%d replacement(s))\n",spot);
[ts2 free];
for (j=-1;j<=num+1;j++) {
ts2 = [tstr copy];
[ts2 replaceRegex:regarr[i] with:"[]" occurrenceNum:j caseSensitive:YES];
printf("replace %dth '%s' with '[]': '%s'\n",j,regarr[i],[ts2 stringValue]);
[ts2 free];
}
printf("Matches '%s'? %d\n",regarr[i],[tstr matchesRegex:regarr[i]]);
printf("----------------------------------------------------------\n");
}
}
[tstr free];
[a free];
[b free];
[c free];
}